package priv.amos.androidnetwork.buffer;

import java.io.ByteArrayOutputStream;
import java.util.Date;

import priv.amos.androidnetwork.type.DataType;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class BufferUtils {
	private static SQLiteDatabase db;
	private static boolean inited;
	public static void saveData(Context ctx,String url,Object data,String type){
		initIfNeed(ctx);
		Cursor c;
		String lastupdate = (new Date()).getTime() + "";
		switch(type){
			case DataType.STRING:
				c = db.rawQuery("select * from string where url=?",
						new String[] { url });
				if (c.moveToNext()) {
					db.execSQL("update string set data=?,lastupdate=? where url=?",
							new Object[] { data, lastupdate, url });
				} else {
					db.execSQL("insert into string values( ?, ?,?)", new Object[] {
							url, data, lastupdate });
				}
				break;
			case DataType.IMAGE:
				if(data instanceof Bitmap){
					Bitmap bitmap=(Bitmap)data;
					final ByteArrayOutputStream os = new ByteArrayOutputStream();
					bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
					c = db.rawQuery("select * from images where url=?",
							new String[] { url });
					if (c.moveToNext()) {
						db.execSQL("update images set img=?,lastupdate=? where url=?",
								new Object[] { os.toByteArray(), url,lastupdate });
					} else {
						db.execSQL("insert into images values( ?, ?,?)", new Object[] { url,
								os.toByteArray(),lastupdate });
					}
				}
				break;
		}
	}
	private synchronized static void init(Context ctx) {
		db = ctx.openOrCreateDatabase("androidnetwork.db", Context.MODE_PRIVATE,
				null);
		String sqldpt = "create table if not exists string(url text,data text,lastupdate text)";
		db.execSQL(sqldpt);
		sqldpt = "create table if not exists images(url text,img BLOB,lastupdate text)";
		db.execSQL(sqldpt);
		inited=true;
	}
	private static void initIfNeed(Context ctx){
		if(!inited){
			init(ctx);
		}
	}
	public static long getLastUpdate(Context ctx,String url){
		initIfNeed(ctx);
		Cursor c = db.rawQuery("select * from string where url=?",
				new String[] { url });
		if (c.moveToNext()) {
			String lastupdate = c.getString(c.getColumnIndex("lastupdate"));
			return Long.parseLong(lastupdate);
		}
		c = db.rawQuery("select * from images where url=?",
				new String[] { url });
		if (c.moveToNext()) {
			String lastupdate = c.getString(c.getColumnIndex("lastupdate"));
			return Long.parseLong(lastupdate);
		}
		return -1;
	}
	public static Object getData(Context ctx,String url,String type){
		initIfNeed(ctx);
		Cursor c;
		switch(type){
			case DataType.STRING:
				c = db.rawQuery("select * from string where url=?",
						new String[] { url });
				if (c.moveToNext()) {
					String data = c.getString(c.getColumnIndex("data"));
					return data;
				}
			break;
			case DataType.IMAGE:
				c = db.rawQuery("select * from images where url=?",
						new String[] { url });
				if (c.moveToNext()) {
					byte[] bytes = c.getBlob(c.getColumnIndex("img"));
					return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
				}
			break;
		}
		return null;
	}
}
